/*
 * @author: wizardc
 */

using Dou.Utils;
using UnityEngine;

namespace Dou.ScreenController
{
    /// <summary>
    /// RectTransform 支持横竖屏切换的适配器工具类
    /// </summary>
    public static class RectTransformAdapterUtil
    {
        public static void CopyFormData(RectTransform origin, RectTransformAdapter adapter, int modeIndex, int fineTuningIndex)
        {
            if (modeIndex >= adapter.rectTransformDataList.Count)
            {
                return;
            }
            
            var data = adapter.rectTransformDataList[modeIndex];
            if (!data.isInit)
            {
                return;
            }
            
            origin.anchoredPosition = data.anchoredPosition;
            origin.sizeDelta = data.sizeDelta;
            origin.anchorMin = data.anchorMin;
            origin.anchorMax = data.anchorMax;
            origin.pivot = data.pivot;
            origin.localEulerAngles = data.localEulerAngles;
            origin.localScale = data.localScale;

            if (fineTuningIndex >= 0 && fineTuningIndex < data.fineTuningList.Count)
            {
                var fineTuning = data.fineTuningList[fineTuningIndex];
                if (fineTuning.anchoredPosition.HasValue)
                {
                    origin.anchoredPosition = fineTuning.anchoredPosition.Value;
                }
                if (fineTuning.sizeDelta.HasValue)
                {
                    origin.sizeDelta = fineTuning.sizeDelta.Value;
                }
                if (fineTuning.anchorMin.HasValue)
                {
                    origin.anchorMin = fineTuning.anchorMin.Value;
                }
                if (fineTuning.anchorMax.HasValue)
                {
                    origin.anchorMax = fineTuning.anchorMax.Value;
                }
                if (fineTuning.pivot.HasValue)
                {
                    origin.pivot = fineTuning.pivot.Value;
                }
                if (fineTuning.localEulerAngles.HasValue)
                {
                    origin.localEulerAngles = fineTuning.localEulerAngles.Value;
                }
                if (fineTuning.localScale.HasValue)
                {
                    origin.localScale = fineTuning.localScale.Value;
                }
            }
        }

#if UNITY_EDITOR
        
        public static void RecodeFormRectTransform(RectTransformData data, RectTransform target)
        {
            data.anchoredPosition = target.anchoredPosition;
            data.sizeDelta = target.sizeDelta;
            data.anchorMin = target.anchorMin;
            data.anchorMax = target.anchorMax;
            data.pivot = target.pivot;
            data.localEulerAngles = target.localEulerAngles;
            data.localScale = target.localScale;
        }

        public static void RecodeFormRectTransform(RectTransformData data, RectTransform target, int fineTuningIndex)
        {
            if (fineTuningIndex >= 0 && fineTuningIndex < data.fineTuningList.Count)
            {
                var fineTuningData = data.fineTuningList[fineTuningIndex];
                if (!MathUtil.CheckEqual(data.anchoredPosition, target.anchoredPosition))
                {
                    fineTuningData.anchoredPosition = target.anchoredPosition;
                }
                if (!MathUtil.CheckEqual(data.sizeDelta, target.sizeDelta))
                {
                    fineTuningData.sizeDelta = target.sizeDelta;
                }
                if (!MathUtil.CheckEqual(data.anchorMin, target.anchorMin))
                {
                    fineTuningData.anchorMin = target.anchorMin;
                }
                if (!MathUtil.CheckEqual(data.anchorMax, target.anchorMax))
                {
                    fineTuningData.anchorMax = target.anchorMax;
                }
                if (!MathUtil.CheckEqual(data.pivot, target.pivot))
                {
                    fineTuningData.pivot = target.pivot;
                }
                if (!MathUtil.CheckEqual(data.localEulerAngles, target.localEulerAngles))
                {
                    fineTuningData.localEulerAngles = target.localEulerAngles;
                }
                if (!MathUtil.CheckEqual(data.localScale, target.localScale))
                {
                    fineTuningData.localScale = target.localScale;
                }
            }
        }
        
#endif
        
    }
}
